<template>
	<view class="flex flex-column flex-1 justify-center align-center position-absolute-full">
		<view v-if="is_bound" class="flex flex-column align-center justify-center">
			<view>
				<image src="@/static/icon/common/succeed.png" style="width: 120rpx;height: 120rpx;border-radius: 20rpx;"></image>
			</view>
			<view class="mt-3">
				<text class=" font-weight-bold ">绑定成功</text>
			</view>
			<view class="mt-3">
				<view @click="navigateBack()" class="flex flex-column align-center justify-center mt-5"
				style="width: 300rpx;height: 80rpx;background-color: #4ECC58;border-radius: 20rpx;">
					<text style="color: #fff;">返回</text>
				</view>
			</view>
			<view style="height: 200rpx;"></view>
		</view>
		<view  v-if="!is_bound" class="flex flex-column align-center justify-center mb-5">
			<view>
				<image :src="wxuserinfo.user_headimgurl" style="width: 120rpx;height: 120rpx;border-radius: 20rpx;"></image>
			</view>
			<view>
				<text class="mt-2 font-weight-bold ">{{wxuserinfo.user_nickname}}</text>
			</view>
			<text class="mt-3 mb-5">确定要绑定当前微信吗？</text>
			<view @click="bindwx()" class="flex flex-column align-center justify-center mt-5"
			style="width: 300rpx;height: 80rpx;background-color: #4ECC58;border-radius: 20rpx;">
				<text style="color: #fff;">绑定</text>
			</view>
		</view>
	</view>
</template>

<script>
	// #ifdef H5
	import wx from 'weixin-js-sdk'
	// #endif
	import {
	  mapMutations,
		mapState
	} from 'vuex'
 let app = {}
  // #ifdef MP-WEIXIN || MP-KUAISHOU
  app = getApp({ allowDefault: true}).$vm
  // #endif
  // #ifdef APP-PLUS || H5
  app = getApp({allowDefault: true})
  // #endif
	export default {
		data() {
			return {
				authtoken:"",
				wxcode:"",
				wxuserinfo:{},
				is_bound:false,
			};
		},
		onLoad(e) {
			if(e.code){
				this.wxcode = e.code
				this.getWxUserinfo()
			}
			wx.enableAlertBeforeUnload({
			    message: "确定要退出吗？",
			    success: function (res) {
			      wx.miniProgram.navigateBack({delta: 1}) 
			    },
			    fail: function (errMsg) {
			      console.log("方法注册失败：", errMsg);
			    },  
			});
		},
		async onShow() {
		},
		onBackPress(options) {
			return false
		},  
		computed:{},
		methods: {
			...mapMutations([
			        'login',   
			  ]),
			  navigateBack(){
				 wx.miniProgram.navigateBack({delta: 1}) 
			  },
			  bindwx(){
				 
				 uni.$http.post("/api/user/bindwx",this.wxuserinfo)
				 .then((res)=>{
				 	//this.wxuserinfo = res.data
					this.is_bound = true
				 	console.log("res",res)
				 })
				 .catch(err=>{
				 	console.log("err",err)
				 }) 
			  },
			getWxUserinfo(){
				uni.showLoading({
					title:"请稍后..."
				})
				let _this = this
				uni.$http.post("/api/user/getWxUserinfo",{
					code:this.wxcode,
				}) 
				.then((res)=>{
					uni.hideLoading()
					_this.wxuserinfo = res.data
					console.log("res",res)
				}) 
				.catch(err=>{
					uni.hideLoading()
					console.log("err",err)
				}) 
			},
		}
	}
	
</script>

<style>
</style>